package com.cqc.jdk8.Jio.writer;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

public class PrintWriterTest {


    /**
     * 构造方法
     */
    @Test
    public void f1() throws Exception {
        String filename = "C:\\Users\\cui\\Desktop\\1.txt";
        PrintWriter writer = new PrintWriter(new File(filename),"utf-8");
        PrintWriter writer2 = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8));
        PrintWriter writer3 = new PrintWriter(new FileOutputStream(filename));
    }

    /**
     * print(str) write(str)的区别：如果str=null,前者会输出null，后者参数不能为null
     * @throws Exception
     */
    @Test
    public void f2() throws Exception {
        String filename = "C:\\Users\\cui\\Desktop\\1.txt";
        PrintWriter writer = new PrintWriter(new File(filename),"utf-8");
        writer.println("中国");
        writer.write("山东");
        writer.println();
        writer.write("济南");
        writer.flush();
        writer.close();
    }
}
